package cn.iocoder.yudao.module.biz.controller.admin.receivables.service.impl;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.module.biz.controller.admin.contract.vo.ContractArchivePageReqVO;
import cn.iocoder.yudao.module.biz.controller.admin.invoice.domain.SanBizInvoiceManagement;
import cn.iocoder.yudao.module.biz.controller.admin.pay.domain.SanBizPaymentManagement;
import cn.iocoder.yudao.module.biz.controller.admin.pay.mapper.SanBizPaymentManagementMapper;
import cn.iocoder.yudao.module.biz.controller.admin.pay.service.SanBizPaymentManagementService;
import cn.iocoder.yudao.module.biz.dal.dataobject.contract.ContractArchiveDO;
import cn.iocoder.yudao.module.biz.dal.mysql.contract.ContractArchiveMapper;
import cn.iocoder.yudao.module.biz.service.contract.ContractArchiveService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.iocoder.yudao.module.biz.controller.admin.receivables.domain.SanBizContractArchiveReceiveRecord;
import cn.iocoder.yudao.module.biz.controller.admin.receivables.service.SanBizContractArchiveReceiveRecordService;
import cn.iocoder.yudao.module.biz.controller.admin.receivables.mapper.SanBizContractArchiveReceiveRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;

/**
* @author 王兴鹏
* @description 针对表【san_biz_contract_archive_receive_record(合同档案-分期收款)】的数据库操作Service实现
* @createDate 2025-08-26 16:47:47
*/
@Service
public class SanBizContractArchiveReceiveRecordServiceImpl extends ServiceImpl<SanBizContractArchiveReceiveRecordMapper, SanBizContractArchiveReceiveRecord>
    implements SanBizContractArchiveReceiveRecordService{

    @Resource
    private SanBizContractArchiveReceiveRecordMapper mapper;

    @Resource
    private ContractArchiveService contractArchiveService;

    @Resource
    private SanBizPaymentManagementService paymentManagementService;

    @Resource
    private ContractArchiveMapper contractArchiveMapper;

    @Override
    public IPage<SanBizContractArchiveReceiveRecord> getContractArchiveReceiveRecordPage(SanBizContractArchiveReceiveRecord pageReqVO) {
        QueryWrapper queryWrapper = new QueryWrapper();
        PageParam pageParam = new PageParam();
        pageParam.setPageNo(pageReqVO.getPageNo());
        pageParam.setPageSize(pageReqVO.getPageSize());
        if(pageReqVO.getReceiveDateStr() != null && pageReqVO.getReceiveDateStr().size()> 1){
            queryWrapper.ge("receive_date",pageReqVO.getReceiveDateStr().get(0));
            queryWrapper.le("receive_date",pageReqVO.getReceiveDateStr().get(1));
        }
        if(StringUtils.isNotEmpty( pageReqVO.getContractArchiveInfo().getContractName())){
            QueryWrapper qc = new QueryWrapper();
            qc.like("contract_name",pageReqVO.getContractArchiveInfo().getContractName());
            QueryWrapper qc2 = new QueryWrapper();
            qc2.like("contract_code",pageReqVO.getContractArchiveInfo().getContractName());
            List<ContractArchiveDO> list = contractArchiveMapper.selectList(qc);
            list.addAll( contractArchiveMapper.selectList(qc2));
            queryWrapper.in("contract_archive_id", list.stream().map(a -> a.getId()));
        }
//        if( pageReqVO.getContractArchiveId() != null){
//            queryWrapper.eq("contract_code", pageReqVO.getContractArchiveId());
//        }
        queryWrapper.eq(StringUtils.isNotBlank(pageReqVO.getStatus()),"status",pageReqVO.getStatus());
        queryWrapper.eq(StringUtils.isNotBlank(pageReqVO.getTaxStatus()),"tax_status",pageReqVO.getTaxStatus());
        IPage<SanBizContractArchiveReceiveRecord> mpPage = MyBatisUtils.buildPage(pageParam);
//        return mapper.selectPage(mpPage, queryWrapper);
        IPage<SanBizContractArchiveReceiveRecord> page = mapper.selectPage(mpPage, queryWrapper);
        page.getRecords().forEach(a -> {
            QueryWrapper w = new QueryWrapper();
            a.setContractArchiveInfo(contractArchiveService.getContractArchive(a.getContractArchiveId()));
           if(a.getContractArchiveInfo()!=null){
               w.eq("contract_code",a.getContractArchiveInfo().getContractCode());
               a.setPayment(paymentManagementService.list(w));
           }
//           if(a.getBadDebtDate() != null || a.getBadDebtReason() != null){
//               a.setStatus("3"); // 坏账
//           }
//           else if(a.getPayment() == null || a.getPayment().size() == 0){
//               a.setStatus("0"); // 未收款
//           } else {
//               a.setStatus("1"); // 已收款
//           }
        });
        return page;
    }

    @Override
    public IPage<SanBizContractArchiveReceiveRecord> getContractArchiveReceiveRecordPage2(SanBizContractArchiveReceiveRecord pageReqVO) {
        IPage<SanBizContractArchiveReceiveRecord> pageResult = new Page<>();
        pageResult.setRecords(mapper.getContractArchiveReceiveRecordPage2(pageReqVO));
        pageResult.setTotal(mapper.getCount(pageReqVO));
        pageResult.setSize(pageReqVO.getPageSize());
        pageResult.setCurrent(pageReqVO.getPageNo());
        return pageResult;
    }

    @Override
    public List<SanBizContractArchiveReceiveRecord> getContractArchiveReceiveRecordList(SanBizContractArchiveReceiveRecord pageReqVO) {
        pageReqVO.setPageNo(1);
        pageReqVO.setPageSize(500);
        List<SanBizContractArchiveReceiveRecord>  list = mapper.getContractArchiveReceiveRecordPage2(pageReqVO);
        list.forEach(a ->{
            BigDecimal count = a.getReceiveMoney();
            BigDecimal sum  = BigDecimal.ZERO;
            for (SanBizPaymentManagement pa : a.getPayment()) {
                sum = sum.add(pa.getPayMoney());
            }
            a.setDisabled(count.compareTo(sum) == 0 || count.compareTo(sum) < 0);

            BigDecimal count2 = a.getReceiveMoney();
            BigDecimal sum2  = BigDecimal.ZERO;
            for (SanBizInvoiceManagement in : a.getInvoice()) {
                sum2 = sum2.add(in.getInvoiceAmount());
            }
            a.setDisabledInvoice(count2.compareTo(sum2) == 0 || count2.compareTo(sum2) < 0);
        });
        return list;
    }

    public static void main(String[] args) {
        // 创建一个 BigDecimal 列表
        List<BigDecimal> numbers = Arrays.asList(
                new BigDecimal("100.50"),
                new BigDecimal("250.25"),
                new BigDecimal("150.75"),
                new BigDecimal("99.99")
        );

        // 方法一：使用循环和 add 方法
        BigDecimal sum = BigDecimal.ZERO; // 初始化为 0，这是最好的起点
        for (BigDecimal number : numbers) {
            sum = sum.add(number); // 将返回值赋给 sum
        }
    }
}




